package com.example.myapplication.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.myapplication.R;

import java.util.List;

public class ViewPagerAdapter extends PagerAdapter {
    private List<View> views;

    public ViewPagerAdapter(List<View> list) {
        this.views = list;
    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        int index = views.indexOf(view);
        RadioGroup radioGroup = view.findViewById(R.id.radioGroup);
        Button button = view.findViewById(R.id.button);
        for (int i = 0; i < views.size(); i++) {
            radioGroup.getChildAt(i).setEnabled(false);
        }
        radioGroup.check(radioGroup.getChildAt(index).getId());
        if (index == views.size() - 1) {
            button.setVisibility(View.VISIBLE);
        }
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(views.get(position));
        return views.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(views.get(position));
    }
}
